package com.dms.controller;

import com.dms.pojo.Building;
import com.dms.service.BuildingManageService;
import com.dms.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @className: BuildingManageController
 * @package: com.dms.controller
 * @description 楼宇管理模块控制层
 * @author: sk
 */
@Controller
@RequestMapping("/buildingManageController")
public class BuildingManageController {

    @Autowired
    BuildingManageService buildingManageService;

    /**
     * 获取楼宇列表数据
     * @param query
     * @param select
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getBuildingLimitList")
    @ResponseBody
    public Msg getBuildingLimitList(@RequestParam(value = "query",required = false) String query, @RequestParam(value = "select",defaultValue = "1") int select, @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        List<Building> buildingList = buildingManageService.getBuildingLimitList(query, select ,pageNum, pageSize);
        return Msg.success().add("buildingList", buildingList).add("total", buildingManageService.getBuildingTotal(query, select));
    }

    /**
     *  获取楼栋列表所有信息
     * @return
     */
    @GetMapping("/getBuildingIdList")
    @ResponseBody
    public Msg getBuildingIdList(){
        return Msg.success().add("buildingList",buildingManageService.getBuildingIdList());
    }

    /**
     *  判断楼栋号是否存在
     * @param buildingNumber
     * @return
     */
    @GetMapping("/checkBuildingNumber")
    @ResponseBody
    public Msg checkBuildingNumber(@RequestParam("buildingNumber") String buildingNumber){
        return !buildingManageService.checkBuildingNumber(buildingNumber) ? Msg.success() : Msg.fail();
    }

    /**
     *  添加新的楼栋
     * @param building
     * @return
     */
    @PostMapping("/addBuilding")
    @ResponseBody
    public Msg addBuilding(@RequestBody Building building){
        System.out.println(building);
        int num = buildingManageService.addBuilding(building);
        if (num > 0)
            return Msg.success();
        return Msg.fail();
    }

    /**
     * 根据 楼栋号查询楼栋信息
     * @param buildingNumber
     * @return
     */
    @GetMapping("/getBuildingByNumber")
    @ResponseBody
    public Msg getBuildingByNumber(@RequestParam("buildingNumber") String buildingNumber){
        return Msg.success().add("buildingInfo", buildingManageService.getBuildingByNumber(buildingNumber));
    }

    /**
     *  根据 楼栋号 修改楼栋信息
     * @param building
     * @return
     */
    @PutMapping("/editBuildingByNumber")
    @ResponseBody
    public Msg editBuildingByNumber(@RequestBody Building building){
        return buildingManageService.editBuildingByNumber(building) > 0 ? Msg.success() : Msg.fail();
    }

    /**
     * 删除楼栋
     * @param buildingId
     * @return
     */
    @DeleteMapping("/deleteBuilding")
    @ResponseBody
    public Msg deleteBuilding(@RequestParam("buildingId") int buildingId){
        return buildingManageService.deleteBuilding(buildingId) > 0 ? Msg.success() : Msg.fail();
    }
}
